package com.cyl.wms.controller;

import com.cyl.wms.domain.Device;
import com.cyl.wms.pojo.query.DeviceQuery;
import com.cyl.wms.pojo.vo.DeviceVO;
import com.cyl.wms.service.DeviceService;
import com.cyl.wms.service.DeviceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备Controller
 * 
 * @author zcc
 * @date 2022-08-05
 */
@Api(description ="设备接口列表")
@RestController
@RequestMapping("/wms/device")
public class DeviceController extends BaseController {
    @Autowired
    private DeviceService service;

    @ApiOperation("查询设备列表")
    @PreAuthorize("@ss.hasPermi('wms:item:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<Device>> list(@RequestBody DeviceQuery query, Pageable page) {
        List<Device> items = service.selectList(query, page);

        return ResponseEntity.ok(new PageImpl<>(items, page, ((com.github.pagehelper.Page)items).getTotal()));
    }

    @ApiOperation("导出设备列表")
    @PreAuthorize("@ss.hasPermi('wms:item:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(DeviceQuery query) {
        List<DeviceVO> list = service.selectVOList(query, null);
        ExcelUtil<DeviceVO> util = new ExcelUtil<>(DeviceVO.class);
        return ResponseEntity.ok(util.writeExcel(list, "设备数据"));
    }

    @ApiOperation("获取设备详细信息")
    @PreAuthorize("@ss.hasPermi('wms:item:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<Device> getInfo(@PathVariable("id") Long id) {
        Device item = service.selectById(id);
        //DeviceVO itemVO = service.toVo(item);
        return ResponseEntity.ok(item);
    }

    @ApiOperation("新增设备")
    @PreAuthorize("@ss.hasPermi('wms:item:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody Device item) {
        return ResponseEntity.ok(service.insert(item));
    }

    @ApiOperation("修改设备")
    @PreAuthorize("@ss.hasPermi('wms:item:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody Device item) {
        return ResponseEntity.ok(service.update(item));
    }

    @ApiOperation("删除设备")
    @PreAuthorize("@ss.hasPermi('wms:item:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
}
